package cn.zclong.reply.web.servlet.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.servlet.BaseServlet;
import cn.zclong.article.domain.Article;
import cn.zclong.article.domain.PageBean;
import cn.zclong.reply.domain.Reply;
import cn.zclong.reply.service.ReplyService;

/**
 * 管理员对帖子的回复的管理
 */
public class AdminReplyServlet extends BaseServlet {
	private ReplyService replyService = new ReplyService();

	/*
	 * 找出所有已经审核过得帖子
	 */
	public String adminFindById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 分页查询所有数据 1，获取页面传递的pc当前页码 2，给定ps的值，每页记录数
		 * 3，使用pc和ps调用的service方法，得到PageBean,保存到request域 4，转发到in.jsp
		 */
		int pc = getPc(request);// 得到pc
		int ps = 5;// 给定ps的值，每页五行记录
		PageBean<Article> pb_article = replyService.adminFindById(pc, ps);// 传递pc和ps给service方法，得到PageBean
		request.setAttribute("pb", pb_article);// 保存到request域
		return "f:/adminjsps/admin/article/reply.jsp";
	}

	/*
	 * 获取pc当前页码
	 */
	public int getPc(HttpServletRequest request) {
		String value = request.getParameter("pc");
		if (value == null || value.trim().isEmpty()) {
			return 1;// pc位null，表示只有一页
		}
		return Integer.parseInt(value);// 把pc转换成int类型
	}

	/*
	 * 根据帖子id查找对应的评论
	 */
	public String adminFindReplyById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 先获取该帖子的id
		String aid = request.getParameter("aid");
		System.out.println(aid);
		/*
		 * 分页查询所有数据 1，获取页面传递的pc当前页码 2，给定ps的值，每页记录数
		 * 3，使用pc和ps调用的service方法，得到PageBean,保存到request域 4，转发到in.jsp
		 */
		int pc = getPc(request);// 得到pc
		int ps = 4;// 给定ps的值，每页四行记录
		PageBean<Reply> pb = replyService.adminFindReplyById(pc, ps, aid);// 传递pc和ps给service方法，得到PageBean

		// 得到url，保存到pb中
		pb.setUrl(getUrl(request));

		request.setAttribute("pb", pb);// 保存到request域
		return "f:/adminjsps/admin/article/reply_list.jsp";
	}

	/*
	 * 截取url /项目名/Servlet路径？参数字符串
	 */
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();// 获取项目名
		String servletPath = request.getServletPath();// 获取servletpath，即/ReplyServlet
		String queryString = request.getQueryString();// 获取问号之后的参数部分

		// 判断参数部分中是否包含pc这个参数，如果包含，截取下来，不需要这一部分
		if (queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		System.out.println(contextPath + servletPath + "?" + queryString);
		return contextPath + servletPath + "?" + queryString;

	}
	
	/*
	 * 更新回复的审核状态
	 */
	public String adminUpdateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String rid = request.getParameter("rid");
		String userId = request.getParameter("userId");
		
		replyService.adminUpdateById(userId, rid);
		
		return adminFindReplyById(request, response);
	}
	
	/*
	 * 删除该回复
	 */
	public String adminDeleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String rid = request.getParameter("rid");
		
		replyService.adminDeleteById(rid);
		
		return adminFindReplyById(request, response);
	}
	
	/*
	 * 删除该帖子的所有回复
	 */
	public String adminDeleteAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		
		replyService.adminDeleteAll(aid);
		
		return adminFindReplyById(request, response);
	}
}
